<?php

require_once dirname(__FILE__) . '/loadphplib.php';
$intMaxWgetNumber = 200;

function getCurrentWgetNumber() {
    $cmd = 'ps -ef | grep wget | wc -l';
    $currentWgetNumber = intval(shell_exec($cmd));
    log::n('Current Wget Number is ' . $currentWgetNumber);
    return $currentWgetNumber;
}

$URLLIBS = getArrayFromFile(dirname(__FILE__) . '/urllibs.txt');

$arrUniqDomainDataWWW = getArrayFromFile(dirname(__FILE__) . '/domainlist.txt');

$arrUnSetDomains = getArrayFromFile(dirname(dirname(__FILE__)) . '/stat/unsetdomain.txt');

foreach ($arrUniqDomainDataWWW as $domainALL) {
    $arrDInfo = explode('.', $domainALL);
    $length = count($arrDInfo);
    $extk = $length - 1;
    $dok = $length - 2;
    $domain = $arrDInfo[$dok] . '.' . $arrDInfo[$extk];
    if (is_numeric($arrDInfo[$extk])) {
        log::w($domain);
        continue;
    }
    if (in_array($domain, $arrUnSetDomains)) {
        continue;
    }
    if (in_array($domainALL, $arrUnSetDomains)) {
        continue;
    }
    if (in_array(str_replace('www.', '', $domainALL), $arrUnSetDomains)) {
        continue;
    }
    $arrUniqDomainDataUniq[] = $domainALL;
}

sort($arrUniqDomainDataUniq);

foreach ($arrUniqDomainDataUniq as $wwwdomain) {
    foreach ($URLLIBS as $URL) {
        $Fetch_URL = str_replace('(*)', $wwwdomain, $URL);

        #$Fetch_URL = str_replace('&', '\&', $Fetch_URL);

        $cmd = 'wget "' . $Fetch_URL . '" --timeout=3 -t1 -O /dev/null';
        $cmd_exec = 'nohup ' . $cmd . ' >> /dev/null 2>&1 &';
        log::n($cmd_exec);

        if (getCurrentWgetNumber() <= $intMaxWgetNumber) {
            shell_exec($cmd_exec);
        }
    }
}
?>